package com.zhangkang.stock.stocklib.view.index

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import com.zhangkang.stock.stocklib.data.KLineInfo
import com.zhangkang.stock.stocklib.view.CanvasTools
import com.zhangkang.stock.stocklib.view.IndexLineInfo
import com.zhangkang.stock.stocklib.view.IndexNameConfig
import com.zhangkang.stock.stocklib.view.KLineIndex
import com.zhangkang.stock.stocklib.view.KLineIndexInfo
import com.zhangkang.stock.stocklib.view.KLineViewColor
import com.zhangkang.stock.stocklib.view.KLineViewDimen
import com.zhangkang.stock.stocklib.view.PathPool
import com.zhangkang.stock.stocklib.view.RectPool

/**
 * @author : Android-张康
 * created on: 2023/5/9 18:19
 * description: 成交量
 */
internal class KLineVolume(
    private val pathPool: PathPool,
    private val rectPool: RectPool,
    private val kLineViewColor: KLineViewColor,
    private val kLineViewDimen: KLineViewDimen,
    private val indexNameConfig: IndexNameConfig
) : KLineIndexInfo {

    private val transactionVolumeMa5 = object : IndexLineInfo {

        override fun getName(): String {
            return "MA5"
        }

        override fun getLineColor(): Int {
            return Color.parseColor("#F09636")
        }

        override fun getIndexValue(kLineInfo: KLineInfo): Float {
            return kLineInfo.volumeMa5
        }
    }

    private val transactionVolumeMa10 = object : IndexLineInfo {

        override fun getName(): String {
            return "MA10"
        }

        override fun getLineColor(): Int {
            return Color.parseColor("#4988DF")
        }

        override fun getIndexValue(kLineInfo: KLineInfo): Float {
            return kLineInfo.volumeMa10
        }
    }

    private val indexLineInfoList = mutableListOf(
        transactionVolumeMa5,
        transactionVolumeMa10
    )

    private val indexLineInfoPath = mutableMapOf<IndexLineInfo, Path>()

    private var maxValue: Long = 0

    private var minValue: Long = 0

    private var priceItem: Float = 0.0F

    private var volumeWidth: Float = 0.0F

    /**
     * 获取K线指标类型
     */
    override fun getKLineIndex(): KLineIndex {
        return KLineIndex.Volume
    }

    override fun getProportion(): Int {
        return 1
    }

    /**
     * 计算最大值前
     *
     * @param kLineInfo 第一个值
     */
    override fun calculationMaxAndMinBefore(kLineInfo: KLineInfo) {
        maxValue = 0
    }

    /**
     * 计算最大值
     *
     * @param kLineInfo 当前值
     */
    override fun calculationMaxAndMin(kLineInfo: KLineInfo) {
        if (kLineInfo.volume > maxValue) {
            maxValue = kLineInfo.volume
        }
    }

    /**
     * 计算最大值后
     */
    override fun calculationMaxAndMinAfter() {

    }

    /**
     * 绘制背景
     *
     * @param canvas 画布
     * @param startX 开始X
     * @param startY 开始Y
     * @param stopX 结束X
     * @param stopY 结束Y
     * @param paint 画布
     */
    override fun drawBackground(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        linePaint: Paint,
        textPaint: Paint
    ) {
        val backgroundPath = pathPool.acquire().apply {
            moveTo(startX, startY)
            lineTo(stopX, startY)
            lineTo(stopX, stopY)
            lineTo(startX, stopY)
            close()
        }
        canvas.drawPath(backgroundPath, linePaint)
        pathPool.release(backgroundPath)
        //绘制最高价
        textPaint.textSize = kLineViewDimen.ordinateTextSize
        textPaint.color = kLineViewColor.ordinateTextColor
        val maxValueText = maxValue.toString()
        val mTextBoundsRect = rectPool.acquire()
        textPaint.getTextBounds(maxValueText, 0, maxValueText.length, mTextBoundsRect)
        val textStartY = mTextBoundsRect.height() + kLineViewDimen.textPaddingLine + startY
        canvas.drawText(
            maxValueText,
            startX + kLineViewDimen.textPaddingLine,
            textStartY,
            textPaint
        )
        rectPool.release(mTextBoundsRect)
    }

    override fun drawCurrentIndexInfo(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        linePaint: Paint,
        textPaint: Paint,
        marginTop: Float,
        span: Int,
        indexValueHorizontalSpace: Float,
        indexValueVerticalSpace: Float,
        kLineInfo: KLineInfo,
        kLineIsLine: Boolean
    ) {
        //先绘制指标名称
        CanvasTools.drawIndicatorName(
            canvas = canvas,
            rectLeft = startX,
            startY = startY,
            stopY = stopY,
            linePaint = linePaint,
            textPaint = textPaint,
            rectPool = rectPool,
            indicatorName = "成交量",
            indexNameConfig = indexNameConfig
        )
        textPaint.textSize = kLineViewDimen.indexValueTextSize
        var indicatorStart = indexNameConfig.width + indexValueHorizontalSpace
        var drawTextInVerticalCenter = 0
        indexLineInfoList.forEach { maInfo ->
            indicatorStart += drawTextInVerticalCenter + indexValueHorizontalSpace
            val currentString = "${maInfo.getName()}:${maInfo.getIndexValue(kLineInfo)}"
            textPaint.color = maInfo.getLineColor()
            drawTextInVerticalCenter = CanvasTools.drawTextInVerticalCenter(
                canvas, indicatorStart, startY, stopY, currentString, textPaint, rectPool = rectPool
            )
            val indexPath = pathPool.acquire()
            indexLineInfoPath[maInfo] = indexPath
        }
    }

    override fun beforeDrawItemContent(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        paint: Paint,
        candlestickWidth: Float,
        candlestickWidthSpace: Float,
        kLineInfo: KLineInfo,
        kLineIsLine: Boolean
    ) {
        val volumeSpace: Float = if (kLineIsLine) {
            0F
        } else {
            kLineViewDimen.volumeRectSpace
        }
        val volumeRange = maxValue - minValue
        val drawHeight = stopY - startY
        priceItem = drawHeight / volumeRange
        volumeWidth = candlestickWidth + candlestickWidthSpace - volumeSpace
    }

    override fun drawItemContent(
        canvas: Canvas,
        startX: Float,
        startY: Float,
        stopX: Float,
        stopY: Float,
        textPaint: Paint,
        linePaint: Paint,
        kLineInfo: KLineInfo,
        isFirst: Boolean,
        yangLineIsFill: Boolean,
        totalTranslateX: Float,
        candlestickWidth: Float,
        halfCandlestickWidth: Float,
        kLineIsLine: Boolean
    ) {
        //最高价格的 Y 坐标
        val volumeY = priceItem * (maxValue - kLineInfo.volume) + startY
        //Path 的横坐标
        var pathX = halfCandlestickWidth + totalTranslateX
        if (pathX < 0) {
            pathX = halfCandlestickWidth
        }
        indexLineInfoPath.forEach {
            val maValue = it.key.getIndexValue(kLineInfo)
            val maY = priceItem * (maxValue - maValue) + startY
            val path = it.value
            if (isFirst) {
                if (maY <= stopY) {
                    path.moveTo(pathX, maY)
                } else {
                    path.moveTo(pathX, stopY)
                }
            } else {
                if (maY < stopY) {
                    path.lineTo(pathX, maY)
                }
            }
        }
        //判断当前绘制的颜色
        val candlestickColor = if (kLineInfo.closePrice > kLineInfo.openPrice) {
            if (!yangLineIsFill) {
                linePaint.style = Paint.Style.STROKE
            }
            kLineViewColor.redColor
        } else if (kLineInfo.closePrice < kLineInfo.openPrice) {
            linePaint.style = Paint.Style.FILL_AND_STROKE
            kLineViewColor.greenColor
        } else {
            linePaint.style = Paint.Style.FILL_AND_STROKE
            kLineViewColor.grayColor
        }
        linePaint.color = candlestickColor
        linePaint.strokeWidth = kLineViewDimen.borderLineStrokeWidth
        val rectLeft: Float = if (totalTranslateX > 0 && !kLineIsLine) {
            startX - kLineViewDimen.volumeRectSpace
        } else {
            startX
        }
        val rectRight: Float = if (isFirst) {
            rectLeft + volumeWidth - kLineViewDimen.borderLineStrokeWidth * 2
        } else {
            rectLeft + volumeWidth
        }
        //绘制矩形
        canvas.drawRect(startX, volumeY, rectRight, stopY, linePaint)
    }

    override fun afterDrawItemContent(canvas: Canvas, paint: Paint) {
        indexLineInfoPath.forEach {
            val maColor = it.key.getLineColor()
            val maPath = it.value
            paint.color = maColor
            canvas.drawPath(maPath, paint)
            pathPool.release(maPath)
        }
        indexLineInfoPath.clear()
    }
}